package com.sx.pipeline.ui.learning

import android.app.Application
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.LoginDTO
import com.sx.pipeline.repos.http.req.MessageDTO
import com.sx.pipeline.repos.http.res.*
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: 学习中心VM
 */
class LearningCenterVM(app: Application) : CYBaseViewModel(app) {

    // 学习中心列表
    val learningCenterListEvent: SingleLiveEvent<Ipage<StudyCenterVO>> = SingleLiveEvent()

    // 消息详情
    val messageDetailEvent: SingleLiveEvent<MessageDetailVo> = SingleLiveEvent()


    /**
     * 获取学习中心列表
     */
    fun getLearningStudy(page: Int = 1) {
        addSubscribe(mModel.getLearningStudy(isPage = true,page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<StudyCenterVO>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: Ipage<StudyCenterVO>) {
                        learningCenterListEvent.postValue(result)
                        if (page == 1) {
                            mUI.initRefreshEvent().postValue(true)
                        } else {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        if (page == 1) {
                            mUI.initRefreshEvent().postValue(false)
                        } else {
                            mUI.initLoadEvent().postValue(false)
                        }
                    }
                })
        )
    }

    /**
     * 获取学习中心详情
     */
    fun getNoticeDetail(id: Int, noticeId: Int) {
        val dto = MessageDTO(id, noticeId)
        addSubscribe(
            mModel.getNoticeDetail(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<MessageDetailVo, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: MessageDetailVo) {
                        messageDetailEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }
}